//
//  ConfLoader.m
//  PatientNew
//
//  Created by xxx on 14-7-11.
//  Copyright (c) 2014年 wtf. All rights reserved.
//

#import "Config.h"
#import "Request.h"

@implementation Config

+ (Config *)standardConfig {
    static Config *standardConfig = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        standardConfig = [[self alloc] init];
        [standardConfig loadAll];
    });
    return standardConfig;
}

- (NSString *)resourceDir
{
    return [[NSBundle mainBundle] resourcePath];
}

- (void)loadAll {
    NSString *configFilePath = [[NSBundle mainBundle] pathForResource:kConfigFileName ofType:kConfigFileType];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:configFilePath]) return;
    
    NSData *data = [NSData dataWithContentsOfFile:configFilePath];
    DDXMLDocument *doc = [[DDXMLDocument alloc] initWithData:data options:0 error:nil];
    DDXMLElement *rootElement = [doc rootElement];
    
    _baseUrlString = [[rootElement elementForName:kXMLNodeNameConfigBaseUrl] stringValue];
    
    _securedBaseUrlString = [[rootElement elementForName:kXMLNodeNameConfigSecuredBaseUrl] stringValue];
    _channel = [[rootElement elementForName:kXMLNodeNameConfigChannel] stringValue];
    
    _requests = [[NSMutableDictionary alloc] init];
    NSArray *requestNodes = [[rootElement elementForName:kXMLNodeNameConfigRequests] elementsForName:kXMLNodeNameConfigRequest];
    for (DDXMLElement *node in requestNodes) {
        Request *request = [Request requestWithXmlNode:node];
        [_requests setObject:request forKey:request.name];
    }
}

@end
